/*
 *	Yalala
 *	
 *	Copyright © MediaMonks B.V.
 *	All rights reserved.
 *	
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions are met:
 *	1. Redistributions of source code must retain the above copyright
 *	   notice, this list of conditions and the following disclaimer.
 *	2. Redistributions in binary form must reproduce the above copyright
 *	   notice, this list of conditions and the following disclaimer in the
 *	   documentation and/or other materials provided with the distribution.
 *	3. All advertising materials mentioning features or use of this software
 *	   must display the following acknowledgement:
 *	   This product includes software developed by MediaMonks B.V.
 *	4. Neither the name of MediaMonks B.V. nor the
 *	   names of its contributors may be used to endorse or promote products
 *	   derived from this software without specific prior written permission.
 *	
 *	THIS SOFTWARE IS PROVIDED BY MEDIAMONKS B.V. ''AS IS'' AND ANY
 *	EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *	DISCLAIMED. IN NO EVENT SHALL MEDIAMONKS B.V. BE LIABLE FOR ANY
 *	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *	
 */

package com.mediamonks.yalala.ui.connections 
{
	import temple.codecomponents.form.components.CodeCheckBox;
	import temple.codecomponents.icons.CodeCross;
	import temple.ui.buttons.BaseButton;
	import temple.ui.tooltip.ToolTip;
	import temple.utils.types.DateUtils;
	import temple.utils.types.DisplayObjectContainerUtils;

	import com.mediamonks.yalala.ui.UIEvent;

	import flash.events.MouseEvent;

	/**
	 * @author Thijs Broerse
	 */
	public class ConnectionButton extends CodeCheckBox 
	{
		private var _id:Number;
		private var _name:String;
		private var _time:String;
		private var _clearButton:BaseButton;

		public function ConnectionButton(id:Number, name:String)
		{
			_id = id;
			_name = name;
			
			if (id) _time = DateUtils.format('H:i:s', new Date(id));
			
			if (id)
			{
				_clearButton = new BaseButton();
				_clearButton.graphics.beginFill(0x000000, 0);
				_clearButton.graphics.drawRect(0, 0, 12, 12);
				var cross:CodeCross = new CodeCross(0xFFFFFF);
				cross.rotation = 45;
				cross.x = 6;
				cross.y = 6;
				_clearButton.addChild(cross);
				addChild(_clearButton);
				_clearButton.y = 4;
				_clearButton.addEventListener(MouseEvent.CLICK, handleClearClick);
				
				ToolTip.add(_clearButton, "Delete all messages of this connection.");
			}
			
			super(formatLabel(), id);
			
			mouseChildren = true;
			DisplayObjectContainerUtils.mouseChildren(this);
			
			if (_clearButton) _clearButton.mouseEnabled = true;
		}

		public function get id():Number
		{
			return _id;
		}
		
		override public function get name():String
		{
			return _name;
		}
		
		override public function set name(value:String):void
		{
			if (_name == value) return;
			_name = value;
			
			label = formatLabel();
		}
		
		override public function set label(value:String):void 
		{
			super.label = value;
			if (_clearButton) _clearButton.x = textField.x + textField.width;
		}

		private function formatLabel():String 
		{
			if (_name && _time)
			{
				return _name + " (" + _time + ")";
			}
			else if (!_name)
			{
				return _time;
			}
			return _name;
		}
		
		private function handleClearClick(event:MouseEvent):void 
		{
			event.stopPropagation();
			dispatchEvent(new UIEvent(UIEvent.CLEAR_CONNECTION, true));
		}

		override public function destruct():void 
		{
			_clearButton = null;
			
			super.destruct();
		}
	}
}
